﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;
using HYSump.DataDefine;
using HYSump.Forms;
using HYSump.Properties;

namespace HYSump
{
	public partial class PlaceSumpForm : System.Windows.Forms.Form
	{
		public UIDocument uiDoc
		{
			get
			{
				return this.m_uiDoc;
			}
		}

		public bool HasChange
		{
			get
			{
				return this.bChange;
			}
			set
			{
				this.bChange = value;
			}
		}

		public bool isExist
		{
			get
			{
				return this.bExist;
			}
		}

		public PlaceSumpForm(UIDocument uiDoc)
		{
			this.bExist = false;
			base.Icon = Resource.YArchitechIcon;
			this.InitializeComponent();
			List<KeyValuePair<Control, bool>> inputControlGroup = new List<KeyValuePair<Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{E3BFA972-AE93-4252-8022-AE8E054677FF}", null);
			this.readFileData();
			this.AdjustControlLocation(this.HoleKind);
			this.ResetOldData();
			this.m_uiDoc = uiDoc;
			this.bChange = false;
			this.bExist = true;
			double num = 1.0;
			double num2 = 1.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
			base.Size = new Size(Convert.ToInt32(224.0 * num), Convert.ToInt32(469.0 * num2));
		}

		private void readFileData()
		{
			string path = Product.UserDataLocation + "\\PlaceSump.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.HoleKind = Type2FileInfo.strRectKind;
				this.SumpType = Type2FileInfo.strIronGrid;
				this.SumpLength = 1000.0;
				this.SumpWidth = 1000.0;
				this.SumpRadius = 500.0;
				this.SumpDepth = 600.0;
				this.SumpThick = 50.0;
				return;
			}
			try
			{
				FileStream fileStream = new FileStream(path, FileMode.Open);
				StreamReader streamReader = new StreamReader(fileStream);
				streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
				base.StartPosition = FormStartPosition.Manual;
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
				this.HoleKind = streamReader.ReadLine();
				this.SumpType = streamReader.ReadLine();
				this.SumpLength = double.Parse(streamReader.ReadLine());
				this.SumpWidth = double.Parse(streamReader.ReadLine());
				this.SumpRadius = double.Parse(streamReader.ReadLine());
				this.SumpDepth = double.Parse(streamReader.ReadLine());
				this.SumpThick = double.Parse(streamReader.ReadLine());
				streamReader.Close();
				fileStream.Close();
			}
			catch (Exception)
			{
				throw;
			}
		}

		private void WriteFileData()
		{
			FileStream fileStream = new FileStream(Product.UserDataLocation + "\\PlaceSump.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			int x = base.Location.X;
			int y = base.Location.Y;
			streamWriter.WriteLine(x.ToString());
			streamWriter.WriteLine(y.ToString());
			streamWriter.WriteLine(this.HoleKind);
			streamWriter.WriteLine(this.SumpType);
			streamWriter.WriteLine(this.cmb_length.Text);
			streamWriter.WriteLine(this.cmb_width.Text);
			streamWriter.WriteLine(this.cmb_radius.Text);
			streamWriter.WriteLine(this.cmb_depth.Text);
			streamWriter.WriteLine(this.cmb_thick.Text);
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void PlaceSumpForm_MouseLeave(object sender, EventArgs e)
		{
		}

		private void cmb_holekind_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.cmb_plan.Items.Clear();
			if (this.cmb_holekind.Text == Type2FileInfo.strRectKind)
			{
				this.cmb_plan.Items.Add(Type2FileInfo.strIronGrid);
				this.cmb_plan.Items.Add(Type2FileInfo.strSolidPlan);
				this.cmb_plan.Items.Add(Type2FileInfo.strIronSeal);
			}
			else if (this.cmb_holekind.Text == Type2FileInfo.stringCircle)
			{
				this.cmb_plan.Items.Add(Type2FileInfo.strCircleGrid);
			}
			this.cmb_plan.SelectedIndex = 0;
			this.AdjustControlLocation(this.cmb_holekind.Text);
			if (this.bExist)
			{
				this.bChange = true;
			}
		}

		private void AdjustControlLocation(string strText)
		{
			double num = 1.0;
			double num2 = 1.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
			if (strText == Type2FileInfo.strRectKind)
			{
				this.groupBox1.Size = new Size((int)(200.0 * num), (int)(138.0 * num2));
				this.cmb_radius.Hide();
				this.lb_radius.Hide();
				this.cmb_length.Show();
				this.cmb_width.Show();
				this.lb_length.Show();
				this.lb_width.Show();
				this.groupBox2.Location = new System.Drawing.Point((int)(4.0 * num), (int)(344.0 * num2));
				base.ClientSize = new Size((int)(210.0 * num), (int)(422.0 * num2));
				return;
			}
			this.groupBox1.Size = new Size((int)(200.0 * num), (int)(104.0 * num2));
			this.cmb_radius.Show();
			this.lb_radius.Show();
			this.lb_radius.Show();
			this.cmb_length.Hide();
			this.cmb_width.Hide();
			this.lb_length.Hide();
			this.lb_width.Hide();
			this.groupBox2.Location = new System.Drawing.Point((int)(4.0 * num), (int)(310.0 * num2));
			base.ClientSize = new Size((int)(210.0 * num), (int)(388.0 * num2));
		}

		public string HoleKind
		{
			get
			{
				return this.cmb_holekind.Text;
			}
			set
			{
				this.cmb_holekind.Text = value;
			}
		}

		public string SumpType
		{
			get
			{
				return this.cmb_plan.Text;
			}
			set
			{
				this.cmb_plan.Text = value;
			}
		}

		public double SumpLength
		{
			get
			{
				return double.Parse(this.cmb_length.Text) / CommonUnit.MM2Feet;
			}
			set
			{
				this.cmb_length.Text = value.ToString();
			}
		}

		public double SumpWidth
		{
			get
			{
				return double.Parse(this.cmb_width.Text) / CommonUnit.MM2Feet;
			}
			set
			{
				this.cmb_width.Text = value.ToString();
			}
		}

		public double SumpRadius
		{
			get
			{
				return double.Parse(this.cmb_radius.Text) / CommonUnit.MM2Feet;
			}
			set
			{
				this.cmb_radius.Text = value.ToString();
			}
		}

		public double SumpDepth
		{
			get
			{
				return double.Parse(this.cmb_depth.Text) / CommonUnit.MM2Feet;
			}
			set
			{
				this.cmb_depth.Text = value.ToString();
			}
		}

		public double SumpThick
		{
			get
			{
				return double.Parse(this.cmb_thick.Text) / CommonUnit.MM2Feet;
			}
			set
			{
				this.cmb_thick.Text = value.ToString();
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void PlaceSumpForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
			this.bExist = false;
			this.WriteFileData();
			if (this.m_hook != null)
			{
				this.m_hook.HookStop();
			}
			this.PressEsc();
		}

		public void PressEsc()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			PlaceSumpForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			PlaceSumpForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void cmb_plan_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (Type2FileInfo.strIronGrid == this.SumpType)
			{
				this.pictureBox1.Image = HYSump.Properties.Resources.铸铁箅子;
			}
			else if (Type2FileInfo.strSolidPlan == this.SumpType)
			{
				this.pictureBox1.Image = HYSump.Properties.Resources.实心混凝土盖板;
			}
			else if (Type2FileInfo.strIronSeal == this.SumpType)
			{
				this.pictureBox1.Image = HYSump.Properties.Resources.密闭型铸铁盖板;
			}
			else if (Type2FileInfo.strCircleGrid == this.SumpType)
			{
				this.pictureBox1.Image = HYSump.Properties.Resources.圆孔箅子;
			}
			if (this.bExist)
			{
				this.bChange = true;
			}
		}

		private void CheckControlModify()
		{
			if (this.m_OldData.dWidth != this.cmb_width.Text)
			{
				this.bChange = true;
			}
			if (this.m_OldData.dLength != this.cmb_length.Text)
			{
				this.bChange = true;
			}
			if (this.m_OldData.dRadius != this.cmb_radius.Text)
			{
				this.bChange = true;
			}
			if (this.m_OldData.dDepth != this.cmb_depth.Text)
			{
				this.bChange = true;
			}
			if (this.m_OldData.dThick != this.cmb_thick.Text)
			{
				this.bChange = true;
			}
		}

		private void ResetOldData()
		{
			this.m_OldData.dWidth = this.cmb_width.Text;
			this.m_OldData.dLength = this.cmb_length.Text;
			this.m_OldData.dRadius = this.cmb_radius.Text;
			this.m_OldData.dDepth = this.cmb_depth.Text;
			this.m_OldData.dThick = this.cmb_thick.Text;
		}

		private void cmb_radius_Validating(object sender, CancelEventArgs e)
		{
			if (this.SumpRadius * CommonUnit.MM2Feet < 100.0)
			{
				MessageBox.Show("半径必须大于等于100mm");
			}
		}

		private void cmb_thick_Validating(object sender, CancelEventArgs e)
		{
			if (this.SumpThick * CommonUnit.MM2Feet < 25.0)
			{
				MessageBox.Show("壁厚必须大于等于25mm");
			}
		}

		private void PlaceSumpForm_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			this.pictureBox1.Focus();
			this.m_hook = new MouseHook(this);
			this.m_hook.HookStart();
		}

		public void CheckIfChanged()
		{
			this.pictureBox1.Focus();
			this.CheckControlModify();
			if (this.bChange)
			{
				int wMsg = 7;
				PlaceSumpForm.SendMessage(CmdPlaceSump._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				this.ResetOldData();
				this.PressEsc();
			}
		}

		private bool bExist;

		private bool bChange;

		private ControlData m_OldData;

		private UIDocument m_uiDoc;

		private MouseHook m_hook;

		private ControlInfoManager m_thisManger;
	}
}
